#!/bin/bash

# Nanomsg Demo Script
# 这个脚本演示了如何使用 nanomsg 的各种通信模式

echo "=== Nanomsg Communication Demo ==="
echo "This script demonstrates different nanomsg communication patterns"
echo ""

# 检查 nanomsg 库是否可用
check_nanomsg() {
    if ! pkg-config --exists nanomsg; then
        echo "Error: nanomsg library not found"
        echo "Please install nanomsg first:"
        echo "  Ubuntu/Debian: sudo apt-get install libnanomsg-dev"
        echo "  CentOS/RHEL: sudo yum install nanomsg-devel"
        exit 1
    fi
    echo "✓ nanomsg library found: $(pkg-config --modversion nanomsg)"
}

# 编译项目
build_project() {
    echo ""
    echo "Building nanomsg demo project..."
    cd "$(dirname "$0")/.."
    
    if make clean && make all; then
        echo "✓ Build successful"
    else
        echo "✗ Build failed"
        exit 1
    fi
}

# 演示 PAIR 模式
demo_pair() {
    echo ""
    echo "=== PAIR Mode Demo ==="
    echo "Starting PAIR communication demo..."
    echo "This will start two nodes that communicate bidirectionally"
    
    # 启动 node1 (后台)
    ./bin/simple_pair node1 &
    NODE1_PID=$!
    sleep 1
    
    # 启动 node2 (前台，运行5秒)
    timeout 5 ./bin/simple_pair node2
    
    # 清理
    kill $NODE1_PID 2>/dev/null
    wait $NODE1_PID 2>/dev/null
    
    echo "PAIR demo completed"
}

# 演示 REQ/REP 模式
demo_reqrep() {
    echo ""
    echo "=== REQ/REP Mode Demo ==="
    echo "Starting Request-Reply communication demo..."
    
    # 启动服务器 (后台)
    ./bin/reqrep_server &
    SERVER_PID=$!
    sleep 1
    
    # 运行批量客户端测试
    echo "Running batch client test..."
    ./bin/reqrep_client batch 5
    
    echo ""
    echo "Running interactive client (type 'quit' to exit)..."
    ./bin/reqrep_client interactive
    
    # 清理
    kill $SERVER_PID 2>/dev/null
    wait $SERVER_PID 2>/dev/null
    
    echo "REQ/REP demo completed"
}

# 性能测试
performance_test() {
    echo ""
    echo "=== Performance Test ==="
    echo "Running performance benchmarks..."
    
    # 启动服务器
    ./bin/reqrep_server &
    SERVER_PID=$!
    sleep 1
    
    echo "Running 10-second performance test..."
    ./bin/reqrep_client perf 10
    
    # 清理
    kill $SERVER_PID 2>/dev/null
    wait $SERVER_PID 2>/dev/null
    
    echo "Performance test completed"
}

# 主菜单
show_menu() {
    echo ""
    echo "Available demos:"
    echo "1. PAIR Mode Demo"
    echo "2. REQ/REP Mode Demo"  
    echo "3. Performance Test"
    echo "4. Build Project"
    echo "5. Exit"
    echo ""
    read -p "Select demo (1-5): " choice
    
    case $choice in
        1) demo_pair ;;
        2) demo_reqrep ;;
        3) performance_test ;;
        4) build_project ;;
        5) echo "Goodbye!"; exit 0 ;;
        *) echo "Invalid choice"; show_menu ;;
    esac
}

# 主函数
main() {
    # 检查依赖
    check_nanomsg
    
    # 如果有参数，直接运行对应的演示
    case "$1" in
        "pair") demo_pair ;;
        "reqrep") demo_reqrep ;;
        "perf") performance_test ;;
        "build") build_project ;;
        "") 
            # 无参数，显示菜单
            while true; do
                show_menu
            done
            ;;
        *)
            echo "Usage: $0 [pair|reqrep|perf|build]"
            echo ""
            echo "Options:"
            echo "  pair   - Run PAIR mode demo"
            echo "  reqrep - Run REQ/REP mode demo"
            echo "  perf   - Run performance test"
            echo "  build  - Build the project"
            echo ""
            echo "Run without arguments for interactive menu"
            exit 1
            ;;
    esac
}

# 信号处理
cleanup() {
    echo ""
    echo "Cleaning up background processes..."
    jobs -p | xargs -r kill
    exit 0
}

trap cleanup SIGINT SIGTERM

# 运行主函数
main "$@"